<?php

namespace app\controller;

use app\BaseController;
use app\model\Admin;
use app\model\Admin as AdminModel;
use app\Request;

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
use app\validate\Index as IndexValidate;

class Index extends BaseController
{
    //获取用户下的所有角色
    public function index()
    {

        try {
            $model = new  AdminModel();
            $data = $model->with('role')->select();
            return show(1,$data);
        } catch (\Exception $e) {
            return show(0,$e->getMessage());
        }
    }
    //查询指定用户下的角色
    public function search(Request $request){

        try {
            $param = $request->param();
            $model = new AdminModel();
            validate(IndexValidate::class)->scene('search')->check($param);
            $role = $model->find($param['id']);
            $data = [];
            if($role){
                $data = $role->role;
            }

            return show(1,$data);
        } catch (\Exception $e) {
           return show(0,$e->getMessage());
        }
    }
    //登录
    public function login(Request $request)
    {
//        $param = $request->param();
//        $res = Admin::where(['username'=>$param['username'],'password'=>$param['password']])->find();
//
//        return show(1,$res);
//     $list=   Admin::with(['profile','orderlist'])->sel
//ect();
//
//       return json($list);
        $param = $request->param();
        if (empty($param['mobile']) || empty($param['password'])) {
            return show(0, '字段错误');
        }
        try {
            validate(IndexValidate::class)->scene('login')->check([
                'mobile' => $param['mobile'],
                'password' => $param['password']
            ]);
            $admin = new Admin;
            //过滤非法字段
            $data = $request->only(['mobile', 'password']);
            $res = $admin->where('mobile', $data['mobile'])->find();

            session('user', $res);
            if ($res) {
                return show(1, $res);
            } else {
                return show(0, '账号或密码错误');
            }

        } catch (ValidateException $e) {
            return show(0, $e->getError());

        }
    }

    //注册
    public function register(Request $request)
    {
        $param = $request->param();
        try {
            validate(IndexValidate::class)->scene('register')->check([
                'mobile' => $param['mobile'],
                'password' => $param['password']
            ]);
            $admin = new Admin;
            //过滤非法字段
            $data = $request->only(['username', 'mobile', 'password']);
            $res = $admin->where('mobile', $data['mobile'])->find();
            if ($res) {
                //注册过了
                return show(0, '已经注册过，请登录');
            } else {
                $admin->save($data);
                return show(1, '注册成功');
            }

        } catch (ValidateException $e) {
            return show(0, $e->getError());

        }

    }


}
